﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class SelectTest : System.Web.UI.Page
    {
        ConnectionSelect CS = new ConnectionSelect();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGrid();
                Fillddl();
            }
        }


        private void FillGrid()
        {
            string Query = "Select * from TBLTEST";
            gvwDetails.DataSource = CS.Select(Query);
            gvwDetails.DataBind();
        }

        protected void gvwDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                TextBox txtName = gvwDetails.FooterRow.FindControl("txtNameF") as TextBox;
                TextBox txtAge = gvwDetails.FooterRow.FindControl("txtAgeF") as TextBox;
                TextBox txtAddress = gvwDetails.FooterRow.FindControl("txtAddressF") as TextBox;

                string Query = " INSERT INTO TBLTEST  VALUES ( (Select Max(Id) From TBLTEST) + 1, '" + txtName.Text + "', " + Convert.ToInt32(txtAge.Text) + ", '" + txtAddress.Text + "' )";
                
                CS.Insert(Query);
                FillGrid();
            }
        }

        protected void gvwDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            ImageButton ibtnDelete = gvwDetails.Rows[e.RowIndex].FindControl("ibtnDelete") as ImageButton;
            string Query = " Delete from TBLTEST WHERE Id = " + Convert.ToInt32(ibtnDelete.CommandArgument) + " ";
            CS.Insert(Query);
            FillGrid();

        }

        protected void gvwDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Label lblName = gvwDetails.Rows[e.NewEditIndex].FindControl("lblName") as Label;
            Label lblAge = gvwDetails.Rows[e.NewEditIndex].FindControl("lblAge") as Label;
            Label lblAddress = gvwDetails.Rows[e.NewEditIndex].FindControl("lblAddress") as Label;

            gvwDetails.EditIndex = e.NewEditIndex;
            FillGrid();

            TextBox txtName = gvwDetails.Rows[e.NewEditIndex].FindControl("txtName") as TextBox;
            TextBox txtAge = gvwDetails.Rows[e.NewEditIndex].FindControl("txtAge") as TextBox;
            TextBox txtAddress = gvwDetails.Rows[e.NewEditIndex].FindControl("txtAddress") as TextBox;         
        }

        protected void gvwDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwDetails.EditIndex = -1;
            FillGrid();
        }

        protected void gvwDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtName = gvwDetails.Rows[e.RowIndex].FindControl("txtName") as TextBox;
            TextBox txtAge = gvwDetails.Rows[e.RowIndex].FindControl("txtAge") as TextBox;
            TextBox txtAddress = gvwDetails.Rows[e.RowIndex].FindControl("txtAddress") as TextBox;


            ImageButton ibtnUpdate = gvwDetails.Rows[e.RowIndex].FindControl("ibtnUpdate") as ImageButton;


            string Query = "Update TBLTEST SET Name = '" + txtName.Text + "', Age = " + txtAge.Text + ", Address = '" + txtAddress.Text + "' where Id = " + Convert.ToInt32(ibtnUpdate.CommandArgument) + " ";
            CS.Insert(Query);

            gvwDetails.EditIndex = -1;
            FillGrid();

        }



        private void Fillddl()
        {
            string Query = "Select Id, Name from TBLTEST";


            ddlName.DataSource = CS.Select(Query);
            ddlName.DataValueField = "Id";
            ddlName.DataTextField = "Name";
            ddlName.DataBind();
        }



    }
}